package com.webwork.common.util;

import java.math.BigDecimal;

/**
 * 数值运算工具类
 *
 */
public class MathUtil {

	private static final int DEF_DIV_SCALE = 3;

	/**
	 * 相加
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double add(double d1, double d2) {
		return add(d1, d2, DEF_DIV_SCALE);
	}
	
	/**
	 * 相加
	 * 
	 * @param d1
	 * @param d2
	 * @param scale
	 * @return
	 */
	public static double add(double d1, double d2, int scale) {
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		b1 = b1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		b2 = b2.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b = b1.add(b2);
		b = b.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return b.doubleValue();
	}
	
	/**
	 * 相减
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double sub(double d1, double d2) {
		return sub(d1, d2, DEF_DIV_SCALE);
	}

	/**
	 * 相减
	 * 
	 * @param d1
	 * @param d2
	 * @param scale
	 * @return
	 */
	public static double sub(double d1, double d2, int scale) {
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		b1 = b1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		b2 = b2.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b = b1.subtract(b2);
		b = b.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return b.doubleValue();
	}

	/**
	 * 相乘
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
//	public static double mul(double d1, double d2) {
//		return mul(d1, d2, DEF_DIV_SCALE);
//	}
	
	/**
	 * 多项相乘
	 * @param num
	 * @return
	 */
	public static double mul(double... num) {
		BigDecimal d = new BigDecimal(Double.toString(1));
		for (double n : num) {
			BigDecimal temp = new BigDecimal(Double.toString(n));
			d = d.multiply(temp);
		}
		d = d.setScale(DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP);
		return d.doubleValue();
	}
	
	/**
	 * 相乘
	 * 
	 * @param d1
	 * @param d2
	 * @param scale
	 * @return
	 */
	public static double mul(double d1, double d2, int scale) {
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		b1 = b1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		b2 = b2.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b = b1.multiply(b2);
		b = b.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return b.doubleValue();
	}

	/**
	 * 相除
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double div(double d1, double d2) {
		return div(d1, d2, DEF_DIV_SCALE);
	}

	/**
	 * 相除
	 * 
	 * @param d1
	 * @param d2
	 * @param scale
	 * @return
	 */
	public static double div(double d1, double d2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		b1 = b1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		b2 = b2.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

}
